package com.fks.fks_service.utils;//package com.fks.fks_service.utils;
//
//import com.alipay.api.AlipayClient;
//import com.alipay.api.AlipayConfig;
//import com.alipay.api.DefaultAlipayClient;
//import com.alipay.api.domain.AlipayTradePayModel;
//import com.alipay.api.request.AlipayTradePayRequest;
//import com.alipay.api.response.AlipayTradePayResponse;
//import com.fks.fks_service.mapper.*;
//import com.fks.fks_service.pojo.*;
//import com.fks.fks_service.utils.thread.DeviceCardProfit;
//import org.apache.http.HttpResponse;
//import org.apache.http.util.EntityUtils;
//import org.quartz.SchedulerException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.scheduling.annotation.EnableAsync;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.math.BigDecimal;
//import java.text.ParseException;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import com.fks.fks_service.mapper.CardMapper;
//import com.fks.fks_service.mapper.AdminMapper;
//import com.fks.fks_service.mapper.UserDBMapper;
//import com.fks.fks_service.mapper.UserCardMapper;
//import com.fks.fks_service.mapper.CardProfitMapper;
//import com.fks.fks_service.mapper.VipMapper;
//import com.fks.fks_service.mapper.ShopOrderMapper;
//
//@Transactional//开启事务回滚
//@Component
//@EnableScheduling
//@EnableAsync
//public class AutomaticUpdate {
//
//    @Autowired
//    CardMapper cardMapper;
//    @Autowired
//    AdminMapper adminMapper;
//    @Autowired
//    UserDBMapper userDBMapper;
//    @Autowired
//    UserCardMapper userCardMapper;
//    @Autowired
//    CardProfitMapper cardProfitMapper;
//    @Autowired
//    VipMapper vipMapper;
//    @Autowired
//    ShopOrderMapper shopOrderMapper;
//
//    @Async
//    @Scheduled(cron = "0 30 11 * * ?")
//    public void scheduledUpdates() throws SchedulerException, ParseException {
//        System.out.println("--------scheduledUpdates start ! ------------"+DateUtils.getNowTime());
//        //计算所有团队积分
//        int number = cardProfitMapper.getAllDeviceCardProfitNumber();
//        if (number>100){
//            int number1 = number/4;
//            int number2 = number%4;
//            List<CardProfit> list1 = cardProfitMapper.getAllDeviceCardProfit(0,number1);
//            List<CardProfit> list2 = cardProfitMapper.getAllDeviceCardProfit(number1,number1);
//            List<CardProfit> list3 = cardProfitMapper.getAllDeviceCardProfit(number1*2,number1);
//            List<CardProfit> list4 = cardProfitMapper.getAllDeviceCardProfit(number1*3,number1+number2);
//            BigDecimal minute = new BigDecimal(1*60*1000);//1分钟
//
//            //创建线程池
//            ExecutorService executorService = Executors.newFixedThreadPool(4);
//
//            //提交任务给线程池执行
//            executorService.submit(new Runnable() {
//                @Override
//                public void run() {
//                    for (CardProfit cardProfit : list1) {
//                        int userId = cardProfit.getDivideIntoUserId();
//                        UserDB user = userDBMapper.getUserById(userId);
//                        BigDecimal integral = new BigDecimal(user.getIntegral());//用户积分
//                        BigDecimal promotePoints = new BigDecimal(user.getPromotePoints());//用户积分
//                        BigDecimal accumulatedPoints = new BigDecimal(user.getAccumulatedPoints());//用户累计积分
//
//                        BigDecimal perMinuteIntegral =new BigDecimal(cardProfit.getPerMinuteIntegral());//每分钟收益
//                        String time = DateUtils.reduce(cardProfit.getExpirationTime(), cardProfit.getLastEffectiveTime());//剩余有效时间
//                        BigDecimal bigDecimal2 = new BigDecimal(time);
//                        if(bigDecimal2.compareTo(new BigDecimal("0"))!=0){
//                            //获取可生效时间
//                            time = DateUtils.reduce(DateUtils.getNowTime(), cardProfit.getLastEffectiveTime());
//                            bigDecimal2 = new BigDecimal(time);
//                            //获取分钟数
//                            BigDecimal bigDecimal = bigDecimal2.divide(minute,BigDecimal.ROUND_CEILING).setScale(0, BigDecimal.ROUND_HALF_UP);
//                            //增加积分
//                            integral = integral.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            promotePoints = promotePoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            accumulatedPoints = accumulatedPoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            //记录生效时间
//                            cardProfitMapper.updateLastEffectiveTime(cardProfit.getId()
//                                    ,DateUtils.timeAddMinute(cardProfit.getLastEffectiveTime(),Integer.parseInt(bigDecimal.toString())));
//                            //修改用户积分信息
//                            userDBMapper.updateUserIntegralAndAccumulatedPoints(user.getId(),integral.toString()
//                                    ,promotePoints.toString(),accumulatedPoints.toString());
//                        }
//                    }
//                }
//            });
//            executorService.submit(new Runnable() {
//                @Override
//                public void run() {
//                    for (CardProfit cardProfit : list2) {
//                        int userId = cardProfit.getDivideIntoUserId();
//                        UserDB user = userDBMapper.getUserById(userId);
//                        BigDecimal integral = new BigDecimal(user.getIntegral());//用户积分
//                        BigDecimal promotePoints = new BigDecimal(user.getPromotePoints());//用户积分
//                        BigDecimal accumulatedPoints = new BigDecimal(user.getAccumulatedPoints());//用户累计积分
//
//                        BigDecimal perMinuteIntegral =new BigDecimal(cardProfit.getPerMinuteIntegral());//每分钟收益
//                        String time = DateUtils.reduce(cardProfit.getExpirationTime(), cardProfit.getLastEffectiveTime());//剩余有效时间
//                        BigDecimal bigDecimal2 = new BigDecimal(time);
//                        if(bigDecimal2.compareTo(new BigDecimal("0"))!=0){
//                            //获取可生效时间
//                            time = DateUtils.reduce(DateUtils.getNowTime(), cardProfit.getLastEffectiveTime());
//                            bigDecimal2 = new BigDecimal(time);
//                            //获取分钟数
//                            BigDecimal bigDecimal = bigDecimal2.divide(minute,BigDecimal.ROUND_CEILING).setScale(0, BigDecimal.ROUND_HALF_UP);
//                            //增加积分
//                            integral = integral.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            promotePoints = promotePoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            accumulatedPoints = accumulatedPoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            //记录生效时间
//                            cardProfitMapper.updateLastEffectiveTime(cardProfit.getId()
//                                    ,DateUtils.timeAddMinute(cardProfit.getLastEffectiveTime(),Integer.parseInt(bigDecimal.toString())));
//                            //修改用户积分信息
//                            userDBMapper.updateUserIntegralAndAccumulatedPoints(user.getId(),integral.toString()
//                                    ,promotePoints.toString(),accumulatedPoints.toString());
//                        }
//                    }
//                }
//            });
//            executorService.submit(new Runnable() {
//                @Override
//                public void run() {
//                    for (CardProfit cardProfit : list3) {
//                        int userId = cardProfit.getDivideIntoUserId();
//                        UserDB user = userDBMapper.getUserById(userId);
//                        BigDecimal integral = new BigDecimal(user.getIntegral());//用户积分
//                        BigDecimal promotePoints = new BigDecimal(user.getPromotePoints());//用户积分
//                        BigDecimal accumulatedPoints = new BigDecimal(user.getAccumulatedPoints());//用户累计积分
//
//                        BigDecimal perMinuteIntegral =new BigDecimal(cardProfit.getPerMinuteIntegral());//每分钟收益
//                        String time = DateUtils.reduce(cardProfit.getExpirationTime(), cardProfit.getLastEffectiveTime());//剩余有效时间
//                        BigDecimal bigDecimal2 = new BigDecimal(time);
//                        if(bigDecimal2.compareTo(new BigDecimal("0"))!=0){
//                            //获取可生效时间
//                            time = DateUtils.reduce(DateUtils.getNowTime(), cardProfit.getLastEffectiveTime());
//                            bigDecimal2 = new BigDecimal(time);
//                            //获取分钟数
//                            BigDecimal bigDecimal = bigDecimal2.divide(minute,BigDecimal.ROUND_CEILING).setScale(0, BigDecimal.ROUND_HALF_UP);
//                            //增加积分
//                            integral = integral.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            promotePoints = promotePoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            accumulatedPoints = accumulatedPoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            //记录生效时间
//                            cardProfitMapper.updateLastEffectiveTime(cardProfit.getId()
//                                    ,DateUtils.timeAddMinute(cardProfit.getLastEffectiveTime(),Integer.parseInt(bigDecimal.toString())));
//                            //修改用户积分信息
//                            userDBMapper.updateUserIntegralAndAccumulatedPoints(user.getId(),integral.toString()
//                                    ,promotePoints.toString(),accumulatedPoints.toString());
//                        }
//                    }
//                }
//            });
//            executorService.submit(new Runnable() {
//                @Override
//                public void run() {
//                    for (CardProfit cardProfit : list4) {
//                        int userId = cardProfit.getDivideIntoUserId();
//                        UserDB user = userDBMapper.getUserById(userId);
//                        BigDecimal integral = new BigDecimal(user.getIntegral());//用户积分
//                        BigDecimal promotePoints = new BigDecimal(user.getPromotePoints());//用户积分
//                        BigDecimal accumulatedPoints = new BigDecimal(user.getAccumulatedPoints());//用户累计积分
//
//                        BigDecimal perMinuteIntegral =new BigDecimal(cardProfit.getPerMinuteIntegral());//每分钟收益
//                        String time = DateUtils.reduce(cardProfit.getExpirationTime(), cardProfit.getLastEffectiveTime());//剩余有效时间
//                        BigDecimal bigDecimal2 = new BigDecimal(time);
//                        if(bigDecimal2.compareTo(new BigDecimal("0"))!=0){
//                            //获取可生效时间
//                            time = DateUtils.reduce(DateUtils.getNowTime(), cardProfit.getLastEffectiveTime());
//                            bigDecimal2 = new BigDecimal(time);
//                            //获取分钟数
//                            BigDecimal bigDecimal = bigDecimal2.divide(minute,BigDecimal.ROUND_CEILING).setScale(0, BigDecimal.ROUND_HALF_UP);
//                            //增加积分
//                            integral = integral.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            promotePoints = promotePoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            accumulatedPoints = accumulatedPoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5,BigDecimal.ROUND_UP));
//                            //记录生效时间
//                            cardProfitMapper.updateLastEffectiveTime(cardProfit.getId()
//                                    ,DateUtils.timeAddMinute(cardProfit.getLastEffectiveTime(),Integer.parseInt(bigDecimal.toString())));
//                            //修改用户积分信息
//                            userDBMapper.updateUserIntegralAndAccumulatedPoints(user.getId(),integral.toString()
//                                    ,promotePoints.toString(),accumulatedPoints.toString());
//                        }
//                    }
//                }
//            });
//            //关闭线程
//            executorService.shutdown();
//        }else if (number!=0) {
//            BigDecimal minute = new BigDecimal(1*60*1000);//1分钟
//            List<CardProfit> list = cardProfitMapper.getAllDeviceCardProfit(0, number);
//            for (CardProfit cardProfit : list) {
//                int userId = cardProfit.getDivideIntoUserId();
//                UserDB user = userDBMapper.getUserById(userId);
//                BigDecimal integral = new BigDecimal(user.getIntegral());//用户积分
//                BigDecimal promotePoints = new BigDecimal(user.getPromotePoints());//用户积分
//                BigDecimal accumulatedPoints = new BigDecimal(user.getAccumulatedPoints());//用户累计积分
//
//                BigDecimal perMinuteIntegral = new BigDecimal(cardProfit.getPerMinuteIntegral());//每分钟收益
//                String time = DateUtils.reduce(cardProfit.getExpirationTime(), cardProfit.getLastEffectiveTime());//剩余有效时间
//                BigDecimal bigDecimal2 = new BigDecimal(time);
//                if (bigDecimal2.compareTo(new BigDecimal("0")) != 0) {
//                    //获取可生效时间
//                    time = DateUtils.reduce(DateUtils.getNowTime(), cardProfit.getLastEffectiveTime());
//                    bigDecimal2 = new BigDecimal(time);
//                    //获取分钟数
//                    BigDecimal bigDecimal = bigDecimal2.divide(minute,BigDecimal.ROUND_CEILING).setScale(0, BigDecimal.ROUND_HALF_UP);
//                    //增加积分
//                    integral = integral.add(bigDecimal.multiply(perMinuteIntegral).setScale(5, BigDecimal.ROUND_UP));
//                    promotePoints = promotePoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5, BigDecimal.ROUND_UP));
//                    accumulatedPoints = accumulatedPoints.add(bigDecimal.multiply(perMinuteIntegral).setScale(5, BigDecimal.ROUND_UP));
//                    //记录生效时间
//                    cardProfitMapper.updateLastEffectiveTime(cardProfit.getId()
//                            , DateUtils.timeAddMinute(cardProfit.getLastEffectiveTime(), Integer.parseInt(bigDecimal.toString())));
//                    //修改用户积分信息
//                    userDBMapper.updateUserIntegralAndAccumulatedPoints(user.getId(), integral.toString()
//                            , promotePoints.toString(), accumulatedPoints.toString());
//                }
//            }
//        }
//
//
//        System.out.println("--------scheduler end ! ------------"+DateUtils.getNowTime());
//    }
//
//    @Async
//    @Scheduled(cron = "0 0 0 * * ?")
//    public void replaceGradeAndDailyTasksNumber() throws SchedulerException, ParseException {
//        System.out.println("--------replaceGradeAndDailyTasksNumber start ! ------------" + DateUtils.getNowTime());
//        //更新每日零撸次数
//        userDBMapper.updateDailyTasksNumber();
//
//        //用户设备数量清零
//        userDBMapper.quantityResetToZero();
//        List<UserCard> userCardList = userCardMapper.getDeviceNumber();
//        //更新用户设备数量
//        for (UserCard userCard : userCardList) {
//            UserDB user = userDBMapper.getUserById(userCard.getUid());
//            userDBMapper.updateDeviceNumber(user.getId(), Integer.valueOf(user.getDeviceNumber())+1);
//        }
//
//        //判断用户等级
//        //1级
//        List<UserDB> list = userDBMapper.getOneUser();
//        for (UserDB user : list) {
//            userDBMapper.replaceGrade(user.getId(),"1","0.02");
//        }
//        //2级
//        list = userDBMapper.getTwoUser();
//        for (UserDB user : list) {
//            userDBMapper.replaceGrade(user.getId(),"2","0.05");
//        }
//        //3级
//        list = userDBMapper.getThreeUser();
//        for (UserDB user : list) {
//            userDBMapper.replaceGrade(user.getId(),"3","0.10");
//        }
//        //4级
//        list = userDBMapper.getFourUser();
//        for (UserDB user : list) {
//            userDBMapper.replaceGrade(user.getId(),"4","0.12");
//        }
//        //5级
//        list = userDBMapper.getFiveUser();
//        for (UserDB user : list) {
//            userDBMapper.replaceGrade(user.getId(),"5","0.15");
//        }
//        //手动设置用户等级
//        List<Vip> vipList = vipMapper.getAllVip();
//        for (Vip vip : vipList) {
//            userDBMapper.replaceGrade(vip.getUid(),vip.getGrade(),vip.getYieldRate());
//        }
//
//        //发放商品积分
//        List<ShopOrder> shopOrderPointsList = shopOrderMapper.getShopOrderPointsList(DateUtils.getNowDay()+" 00:00:00");
//        for (ShopOrder shopOrder : shopOrderPointsList) {
//            UserDB user = userDBMapper.getUserById(shopOrder.getUid());
//            BigDecimal integral = new BigDecimal(user.getIntegral());
//            BigDecimal shopPoints = new BigDecimal(user.getShopPoints());
//            BigDecimal accumulatedPoints = new BigDecimal(user.getAccumulatedPoints());
//            BigDecimal addIntegral = new BigDecimal(shopOrder.getShopPrice())
//                    .multiply(new BigDecimal(0.033))
//                    .setScale(5,BigDecimal.ROUND_UP);
//            userDBMapper.updateIntegralAndShopPoints(user.getId()
//                    ,integral.add(addIntegral).toString()
//                    ,shopPoints.add(addIntegral).toString()
//                    , accumulatedPoints.add(addIntegral).toString());
//            //增加商品时间
//            shopOrderMapper.updateLastEffectiveTime(shopOrder.getId()
//                    ,DateUtils.timeAddDay(shopOrder.getLastEffectiveTime(),1));
//        }
//
//
//    }
//
//
//    @Async
//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void test(){
//        System.out.println("--------test start ! ------------" + DateUtils.getNowTime());
//
//    }
//
//
//
//}
//
//
